import { ElMessage, ElMessageBox } from 'element-plus'
import { IMultiData } from '@/types/multi_data'
import { IGeoFile } from '@/types/geofile'
import { useDayjsHelper, useLoading, usePagination, usePreview } from '@/hooks'
import { apiList } from '@/api/apiList'
import { ref } from 'vue'
import useResourceHelper from '../ui/useResourceHelper'
import { isMsgOK, isOK } from '@/utils/resUtils'

function useGeoFile() {
    const geofileList = ref<IGeoFile[]>([])

    const { getNewPagination, resetPagination, setPagination } = usePagination()

    const pagination = getNewPagination()

    const searchText = ref('')

    const { getMonthRange } = useDayjsHelper()

    const [start_time, end_time] = getMonthRange(6)
    // as [number, number]

    const searchDate = ref<[number, number]>([start_time, end_time])

    const { getStaticUrl } = useResourceHelper()

    const { loading, stop, tableLoading } = useLoading()

    const searchCatalogOrTargetId = ref('')

    const searchCatalogOrTarget = ref<'catalog' | 'target' | ''>('')

    async function getGeofileList() {
        // do post

        console.log('getGeofileList do post')

        const { current, size } = pagination.value

        const [start_time, end_time] = searchDate.value! || [null, null]

        const params: Parameters<typeof apiList.geofileApi.queryPageImage>[0] =
            searchCatalogOrTarget.value == 'catalog'
                ? {
                      page_index: current,
                      page_size: size,
                      keywords: searchText.value || undefined,
                      start_time: start_time || undefined,
                      end_time: end_time || undefined,
                      catalog_id: searchCatalogOrTargetId.value,
                  }
                : {
                      page_index: current,
                      page_size: size,
                      keywords: searchText.value || undefined,
                      start_time: start_time || undefined,
                      end_time: end_time || undefined,
                      target_id: searchCatalogOrTargetId.value,
                  }

        loading()

        try {
            const res = await apiList.queryPageImage(params)

            geofileList.value = res.result?.records || []

            setPagination(pagination, res.result!)

            stop()
            return res
        } catch (error) {
            console.warn('请求异常', error)
        }

        return null
    }

    function searchGeofile() {
        // do post

        console.log('searchGeofile do post')

        resetPagination(pagination)

        getGeofileList()
    }

    async function handleDelete(data: IGeoFile) {
        await ElMessageBox.confirm('确定删除此文件么?')

        try {
            const res = await apiList.geofileApi.deleteGeoFile({
                id: data.id,
            })
            if (isMsgOK(res)) {
                ElMessage.success('删除成功')
                getGeofileList()
            } else {
                ElMessage.error('删除失败')
            }
        } catch (error) {
            console.warn('请求异常')
        }
    }

    const { getPreviewType, canPreview } = usePreview()

    const previewImgUrlRef = ref('')
    const showPreview = ref(false)
    function handlePreview(data: IMultiData) {
        const previewType = getPreviewType(data)

        const { type, can, url } = previewType

        if (can) {
            previewImgUrlRef.value = url

            showPreview.value = true
        } else {
            ElMessage.error('图像无法展示')
        }
    }
    const previewTifUrlRef = ref('')
    function handlePreviewTif(data: IMultiData) {
        const { filepath, relpath } = data
        previewTifUrlRef.value = filepath

        showPreview.value = true
    }

    function handleDownload(data: IGeoFile) {
        const path = getStaticUrl(data.filename)
    }

    const activeDate = ref('2022-03-01')

    function onUpdateActiveDate(value: string) {
        activeDate.value = value
    }

    function getGeofileStatus(data: IGeoFile) {
        const { status } = data

        switch (status) {
            case '01':
                return '已处理'
            case '00':
                return '未处理'
            default:
                return '未知 '
        }
    }

    return {
        geofileList,
        pagination,
        searchText,
        searchDate,
        searchCatalogOrTargetId,
        searchCatalogOrTarget,
        getGeofileList,
        searchGeofile,
        activeDate,
        onUpdateActiveDate,
        tableLoading,

        // preview
        showPreview,
        handlePreview,
        previewImgUrlRef,

        previewTifUrlRef,
        handlePreviewTif,

        canPreview,
        handleDelete,
        getGeofileStatus,
    }
}

export default useGeoFile
